package com.blacktry.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.pojo.Reader;
import com.blacktry.pojo.RespBean;
import com.blacktry.pojo.User;
import com.blacktry.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author blackTry
 * @since 2021-01-31
 */
@RestController
@RequestMapping("/reader")
public class ReaderController {
    @Autowired
    private ReaderService readerService;

    @PostMapping("/save")
    public RespBean save(@RequestBody Reader reader){
        if(readerService.save(reader)){
            return RespBean.success("添加成功！");
        }else{
            return RespBean.error("添加失败！");
        }
    }

    @DeleteMapping("/delete/{id}")
    public RespBean delete(@PathVariable("id") Integer readerId){
        if(readerService.removeById(readerId)){
            return RespBean.success("删除成功！");
        }else{
            return RespBean.error("删除失败！");
        }
    }

    @PutMapping("/update")
    public RespBean update(@RequestBody Reader reader){
        if(readerService.updateById(reader)){
            return RespBean.success("添加成功！");
        }else{
            return RespBean.error("添加失败！");
        }
    }

    @GetMapping("/findAll/{currentPage}/{size}")
    public Page<Reader> findAll(@PathVariable("currentPage")Integer currentPage, @PathVariable("size")Integer size){
        return readerService.selectPage(currentPage,size);
    }

    @GetMapping("findById/{id}")
    public Reader findById(@PathVariable("id")Integer id){
        return readerService.getById(id);
    }
}

